package jp.co.menox.controller.tmserver.lend;

import java.util.Calendar;

import jp.co.menox.controller.tmserver.TmServerControllerBase;
import jp.co.menox.tmcommon.common.DataConst;
import jp.co.menox.tmcommon.model.LendingHistory;
import jp.co.menox.tmserver.service.CategoryService;
import jp.co.menox.tmserver.service.LendingHistoryService;

import org.slim3.controller.Navigation;

/**
 * 貸出承認／却下処理
 * @author
 *
 */
public class LendingUpdateController extends TmServerControllerBase {

    //protected LendingService service;
    protected LendingHistoryService lendingHistoryservice;
    protected CategoryService categoryService;
    {
        //service = new LendingService();
        lendingHistoryservice = new LendingHistoryService();
        categoryService = new CategoryService();
    }

    @Override
    public Navigation run() throws Exception {

        Long lendingHId = this.asLong("lendingHId");
        int intStatus = this.asInteger("statusType");

        LendingHistory lendingHistory = lendingHistoryservice.select(lendingHId);

        if (intStatus == 1) {
            // 承認
            lendingHistory.setStatusType(DataConst.STATUS_SYOUNINZUMI);
            Calendar current = Calendar.getInstance();
            current.set(Calendar.HOUR_OF_DAY, 0);
            current.set(Calendar.MINUTE, 0);
            current.set(Calendar.SECOND, 0);
            lendingHistory.setApprovalDate(current.getTime());
        } else if (intStatus == 5) {
            // 申請却下
            lendingHistory.setStatusType(DataConst.STATUS_SHINSEIKYAKKA);
            Calendar current = Calendar.getInstance();
            current.set(Calendar.HOUR_OF_DAY, 0);
            current.set(Calendar.MINUTE, 0);
            current.set(Calendar.SECOND, 0);
            lendingHistory.setRejectedDate(current.getTime());
        }
        
        lendingHistoryservice.update(lendingHistory);
        
        requestScope("lendingHistory", lendingHistory);
        
        return forward("lendingApproveResult.jsp");
    }

}